package com.leantech.android.browser.utils

import android.content.ClipData
import android.os.Build
import splitties.systemservices.clipboardManager

fun copyToClipboard(text: CharSequence) {
    clipboardManager.setPrimaryClip(ClipData.newPlainText("content", text))
}

fun getClipboardText(): CharSequence {
    clipboardManager.primaryClip?.let { clipData ->
        if (clipData.itemCount > 0) {
            val primaryText = clipData.getItemAt(0).text
            return if (primaryText!= null && checkUrl(primaryText)) {
                primaryText
            } else {
                ""
            }
        }
        return ""
    }
    return ""
}

fun clearClipboard() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        if(clipboardManager.hasPrimaryClip()) {
            clipboardManager.clearPrimaryClip()
        }
    } else {
        clipboardManager.setPrimaryClip(ClipData.newPlainText("",""))
    }
}
